package com.qiantu.phone.ui.activity;

import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.ui.adapter.OpenPageSettingAdapter;
import com.qiantu.phone.ui.adapter.RoomShowSettingAdapter;
import com.qiantu.phone.utils.SPUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RoomShowSettingActivity extends AppActivity {
    private RecyclerView list;
    private RoomShowSettingAdapter adapter;
    private Integer selectKey;
    private Map<String,String> firstShowMap;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_home_page_setting;
    }

    @Override
    protected void initView() {
        list = findViewById(R.id.list);
        list.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new RoomShowSettingAdapter(getContext()){
            @Override
            public void onSelectChanged(Integer selected) {
                selectKey = selected;
            }
        };
        list.setAdapter(adapter);
    }

    @Override
    protected void initData() {
        List<Integer> strings = new ArrayList<>();
        strings.add(0);
        strings.add(1);
        adapter.setData(strings);
        String firstShow =  SPUtils.getInstance().getString ("firstShow");
        if(firstShow.equals("")){
            firstShowMap = new HashMap<>();
        }else{
            firstShowMap = JSON.parseObject(firstShow,new TypeReference<HashMap<String, String>>() {});
        }
        if(firstShowMap.containsKey(AppApplication.getInstance().getCurrentHouseSerialNo()) && firstShowMap.get(AppApplication.getInstance().getCurrentHouseSerialNo()).equals("0")){
            selectKey = 0;
            adapter.setSelected(0);
        }else{
            selectKey = 1;
            adapter.setSelected(1);
        }

    }

    @Override
    public void onRightClick(View view) {
        firstShowMap.put(AppApplication.getInstance().getCurrentHouseSerialNo(),""+selectKey);
        SPUtils.getInstance().put("firstShow", JSON.toJSONString(firstShowMap));
        finish();
    }
}